/**
 * 实现Email的自动补全功能
 */
var nowid;
var totalid; //补全数组的长度
var can1press = false;  //myemail是否打开
var emailafter;
var emailbefor;
$(document).ready(function(){
 $("#email").focus(function(){ //文本框获得焦点，插入Email提示层
	 $("#myemail").remove();
	 $(this).after("<div id='myemail' style='width:220px; height:auto; background:#fff; color:#6B6B6B; position:absolute; left:"+$(this).get(0).offsetLeft+"px; top:"+($(this).get(0).offsetTop+$(this).height() + 12)+"px; border:1px solid #ccc; border-radius:8px; z-index:99'></div>");
	 if($("#myemail").html()){
	    $("#myemail").css("display","block");
	    $(".newemail").css("width",$("#myemail").width());
	    can1press = true;
	 } else {
	    $("#myemail").css("display","none");
	    can1press = false;
	 }  
 }).keyup(function(){ //文本框输入文字时，显示Email提示层和常用Email
	  var press = $("#email").val();
	  if(press!= "" || press!= null){
		  var emailtxt = "";   
		  var emailvar = new Array("@163.com","@qq.com","@sina.com", "@126.com", "@foxmail.com");
		  totalid = emailvar.length;
		  var emailmy = "<div class='newemail' id='newemail_0' onmouseout=\"sbjg('out' ,'0')\" onmouseover=\"sbjg('over' ,'0')\" style='width:220px;cursor:pointer; color:#6B6B6B; overflow:hidden;'><font color='#0099CC'>" + press + "</font></div>";
		  if(!(isEmail(press))){ //没有输入@
		    for(var i=0; i < emailvar.length; i ++) {
		      emailtxt = emailtxt + "<div class='newemail' id='newemail_" + (i + 1) + "' onmouseout=\"sbjg('out' ,'" + (i + 1) + "')\" onmouseover=\"sbjg('over' ,'" + (i + 1) + "')\" style='width:220px;cursor:pointer; color:#6B6B6B; overflow:hidden;'><font color='#0099CC'>" + press + "</font>" + emailvar[i] + "</div>";
		    }
		  } else {
		    emailbefor = press.split("@")[0];
		    emailafter = "@" + press.split("@")[1];
		    for(var i=0; i < emailvar.length; i ++) {
		      var theemail = emailvar[i];
		      if(theemail.indexOf(emailafter) == 0) {
		         emailtxt = emailtxt + "<div class='newemail' id='newemail_" + (i + 1) + "' onmouseout=\"sbjg('out' ,'" + (i + 1) + "')\" onmouseover=\"sbjg('over' ,'" + (i + 1) + "')\" style='width:220px;cursor:pointer; color:#6B6B6B; overflow:hidden;'><font color='#0099CC'>" + emailbefor + "</font>" + emailvar[i] + "</div>";
		      }
		    }
		  }
		  $("#myemail").html(emailmy + emailtxt);//追加到 myemail中
		  if($("#myemail").html()){
		    $("#myemail").css("display","block");
		    $(".newemail").css("width",$("#myemail").width());
		    can1press = true;
		  } else {
		    $("#myemail").css("display","none");
		    can1press = false;
		  }
	  }
	  if(press == "" || press == null){
	    $("#myemail").html("");  
	    $("#myemail").css("display","none"); 
	  }
 });
 $(document).click(function(){ //当点击网页任意地方时 删除补全框
	 if(can1press){
	   $("#myemail").remove();
	   can1press = false;
	   if($("#email").focus()){
	     can1press = false;
	   }
	 }
 });
});
function sbjg(ok, id){
	$("#newemail_" + id).bind("click", function(){ //鼠标点击Email时，文本框内容替换成该条Email，并删除提示层
		 var newhtml = $(this).html();
		 newhtml = newhtml.replace(/<.*?>/g,"");
		 $("#email").val(newhtml);
		 $("#myemail").remove();
	 });
	if(ok == "over"){ //鼠标经过
		 $("#newemail_" + id).css("background","#CACACA");
		 $("#newemail_" + id).focus();
		 nowid = $("#newemail_" + id).index();
	} else {
		$("#newemail_" + id).css("background", "#FFF");
	}
}
//检查email邮箱
function isEmail(str){
 if(str.indexOf("@") > 0){ 
    return true;
  } else {
    return false;
  }
}